package cn.cosmosx.base.util;

import java.io.InputStream;

public final class BytesUtil {

    public static String readBytesToHex(InputStream in, byte[] buff) {
        readBuff(in, buff);
        return bytesToHex(buff);
    }

    public static boolean readBuff(InputStream in, byte[] buff) {
        try {
            return in.read(buff, 0, buff.length) != -1;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public static String bytesToHex(byte[] buff) {
        if (buff == null || buff.length == 0) {
            return null;
        }
        StringBuilder s = new StringBuilder();
        for (byte b : buff) {
            String hv = Integer.toHexString(b & 0xFF);
            if (hv.length() < 2) {
                s.append("0");
            }
            s.append(hv);
        }
        return s.toString().toUpperCase();
    }
}

